package com.wjk.easy.ui;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.wjk.easy.R;
import com.wjk.easy.db.LastUserBean;
import com.wjk.easy.ui.login.LoginActivity;
import com.wjk.easy.utils.D;

import java.util.ArrayList;

public class SplashActivity extends AppCompatActivity {
    LastUserBean currentUser;
    Animation animation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        animation = AnimationUtils.loadAnimation(this, R.anim.splash);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                currentUser = D.queryCurrentUser(SplashActivity.this);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if(currentUser!=null){
                    Intent  intent = new Intent(SplashActivity.this, MainActivity.class);
                    intent.putExtra("lastUserId",currentUser.getUserId());
                    intent.putExtra("lastUserName",currentUser.getUsername());
                    startActivity(intent);
                    finish();
                }
            }
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //findViewById(R.id.page).startAnimation(animation);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            ) {
                showTipDialog();
            } else {
                findViewById(R.id.page).startAnimation(animation);
            }
        } else {
            findViewById(R.id.page).startAnimation(animation);
        }
       /* new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                checkLoginState();
            }
        },3000); */
    }

    private void checkLoginState() {
        LastUserBean currentUser = D.queryCurrentUser(this);
        if(currentUser!=null){
            Intent  intent = new Intent(this, MainActivity.class);
            intent.putExtra("lastUserId",currentUser.getUserId());
            intent.putExtra("lastUserName",currentUser.getUsername());
            startActivity(intent);
            finish();
        }
    }

    private void showTipDialog() {
        new AlertDialog.Builder(this)
                .setTitle("开启蓝牙相关的权限")
                .setMessage("为了连接与管理蓝牙外设，请在接下来的弹窗中点击同意")
                .setCancelable(false)
                .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        getPermissions();
                    }
                }).show();
    }


    private final int SDK_PERMISSION_REQUEST = 127;

    /**/
/*<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"*/

    @TargetApi(23)
    private void getPermissions() {
        ArrayList<String> permissions = new ArrayList<>();

        if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }

        if (permissions.size() > 0) {
            requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
        }else{
            findViewById(R.id.page).startAnimation(animation);
        }
    }
    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        findViewById(R.id.page).startAnimation(animation);
    }
}
